/*
 * Created on 17-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hotel.ejb;

import hotel.interfaces.*;
import hotel.exception.*;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import javax.ejb.CreateException;

/**
 * @ejb.bean name="ReservaFacade"
 *           display-name="Name for ReservaFacade"
 *           description="Description for ReservaFacade"
 *           jndi-name="ejb/ReservaFacade"
 *           type="Stateless"
 *           view-type="both"
 * 
 * @ejb.util generate="physical"
 */
public class ReservaFacadeBean implements SessionBean {
	
	ReservaLocalHome reservaHome = null;
	ClientLocalHome clientHome = null;
	HabitacioLocalHome habitacioHome = null;

	/**
	 * 
	 */
	public ReservaFacadeBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext ctx)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub

	}

	/**
	 * Default create method
	 * 
	 * @throws CreateException
	 * @ejb.create-method
	 */
	public void ejbCreate() throws CreateException {
		// TODO Auto-generated method stub
		try {
			reservaHome = ReservaUtil.getLocalHome();
			clientHome = ClientUtil.getLocalHome();
			habitacioHome = HabitacioUtil.getLocalHome();
		} catch (Exception e) {
			throw new CreateException(e.getMessage());
		}
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setDates(Integer ID, Date desde, Date fins) throws FinderException {
		// TODO Auto-generated method stub
		ReservaLocal r = reservaHome.findByPrimaryKey(new ReservaPK(ID));
		r.setDesdeData(desde);
		r.setFinsData(fins);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getDesdeData(Integer ID) throws FinderException {
		// TODO Auto-generated method stub
		ReservaLocal r = reservaHome.findByPrimaryKey(new ReservaPK(ID));
		return r.getDesdeData();
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getFinsData(Integer ID) throws FinderException {
		// TODO Auto-generated method stub
		ReservaLocal r = reservaHome.findByPrimaryKey(new ReservaPK(ID));
		return r.getFinsData();
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void reservar(Integer ID, Integer clientDNI, Collection habitacionsID, Date desde, Date fins) throws FinderException, CreateException, JaReservatException {
		// TODO Auto-generated method stub
		
		// Construim coleccio d'habitacions
		Collection habitacions = new Vector();
		Iterator it = habitacionsID.iterator();
		while (it.hasNext()) {
			HabitacioLocal habitacio = habitacioHome.findByPrimaryKey(new HabitacioPK((Integer)it.next()));
			// Per cada habitacio que es vol reservar comprovem que es possible
			Collection reservesHab = habitacio.getReserves();
			Iterator itR = reservesHab.iterator();
			while(itR.hasNext()) {
				ReservaLocal reservaHab = (ReservaLocal)itR.next();
				Date desdeD = reservaHab.getDesdeData();
				Date finsD = reservaHab.getFinsData();
				// Si el periode escullit xoca amb el periode d'una antiga reserva, excepcio
				if (! (((desde.compareTo(desdeD) < 0) && (fins.compareTo(desdeD) <0))
					|| ((desde.compareTo(desdeD) > 0) && (desde.compareTo(desdeD) > 0)))) {
					throw new JaReservatException("Habitacio "+ habitacio.getID() + " ja reservada.");
				}
			}
			habitacions.add(habitacio);
		}
		
		ClientLocal client = clientHome.findByPrimaryKey(new ClientPK(clientDNI));
		
		ReservaValue vReserva = new ReservaValue();
		vReserva.setID(ID);
		vReserva.setDesdeData(desde);
		vReserva.setFinsData(fins);
		ReservaLocal reserva = reservaHome.create(vReserva);
		reserva.setClient(client);
		reserva.setHabitacions(habitacions);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void anular(Integer ID) throws RemoveException, FinderException {
		// TODO Auto-generated method stub
		ReservaLocal r = reservaHome.findByPrimaryKey(new ReservaPK(ID));
		r.remove();
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Collection llistat() throws FinderException {
		// TODO Auto-generated method stub
		Collection llistat = new Vector();
		
		Collection c = reservaHome.findAll();
		Iterator it = c.iterator();
		while(it.hasNext()) {
			ReservaLocal reserva = (ReservaLocal)it.next();
			// Llistat de ClientValue
			ReservaValue vReserva = new ReservaValue();
			vReserva.setID(reserva.getID());
			vReserva.setDesdeData(reserva.getDesdeData());
			vReserva.setFinsData(reserva.getFinsData());
			llistat.add(vReserva);
		}
		return llistat;
	}

}